package com.keda.springdata_elasticsearch.controller;

import com.keda.springdata_elasticsearch.entity.ProductDocument;
import com.keda.springdata_elasticsearch.page.Page;
import com.keda.springdata_elasticsearch.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @Auther: liuyanhui
 * @Date: 2023/01/19/ 14:51
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private ProductService productService;

    /**
     * 新增 / 修改索引
     *
     * @return
     */
    @PostMapping("/save")
    public String add(@RequestBody ProductDocument productDocument) {
        productService.save(productDocument);
        return "success";
    }


    /**
     * 删除索引
     *
     * @return
     */
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable String id) {
        productService.delete(id);
        return "success";
    }

    /**
     * 清空索引
     *
     * @return
     */
    @RequestMapping("/deleteAll")
    public String deleteAll() {
        productService.deleteAll();
        return "success";
    }


    /**
     * 根据ID获取
     *
     * @return
     */
    @RequestMapping("/get/{id}")
    public ProductDocument getById(@PathVariable String id) {
        return productService.getById(id);
    }

    /**
     * 根据获取全部
     *
     * @return
     */
    @RequestMapping("/getAll")
    public List<ProductDocument> getAll() {
        return productService.getAll();
    }

    /**
     * 搜索
     *
     * @param keyword
     * @return
     */
    @RequestMapping("/query/{keyword}")
    public List<ProductDocument> query(@PathVariable String keyword) {
        return productService.query(keyword, ProductDocument.class);
    }

    /**
     * 搜索，命中关键字高亮
     * http://localhost:8080/query_hit?keyword=无印良品荣耀&indexName=orders&fields=productName,productDesc
     *
     * @param keyword   关键字
     * @param fields    搜索字段名称，多个以“，”分割
     * @return
     */
    @RequestMapping("/query_hit")
    public List<ProductDocument> queryHit(@RequestParam String keyword,  @RequestParam String fields) {
        String[] fieldNames = getFields(fields);
        return productService.queryHit(keyword, fieldNames, ProductDocument.class);
    }

    private String[] getFields(String fields) {
        String[] fieldNames;
        if (fields.contains(",")) {
            fieldNames = fields.split(",");
        } else {
            fieldNames = new String[]{fields};
        }
        return fieldNames;
    }

    /**
     * 搜索，命中关键字高亮
     * http://localhost:8080/query_hit_page?keyword=无印良品荣耀&indexName=orders&fields=productName,productDesc&pageNo=1&pageSize=1
     *
     * @param pageNo    当前页
     * @param pageSize  每页显示的数据条数
     * @param keyword   关键字
     * @param fields    搜索字段名称，多个以“，”分割
     * @return
     */
    @RequestMapping("query_hit_page")
    public Page<List<ProductDocument>> queryHitByPage(@RequestParam int pageNo, @RequestParam int pageSize
            , @RequestParam String keyword, @RequestParam String fields) {
        String[] fieldNames = getFields(fields);
        Page<List<ProductDocument>> productDocumentPage = productService.queryHitByPage(pageNo, pageSize, keyword, fieldNames, ProductDocument.class);
        return productDocumentPage;
    }

    /**
     * 删除索引库
     * @return
     */
    @RequestMapping("delete_index")
    public String deleteIndex(){
        productService.deleteIndex(ProductDocument.class);
        return "success";
    }
}
